import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;

/**
 * Write a description of class TractorBeam here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TractorBeam extends Attachable
{
    /**
     * Act - do whatever the TractorBeam wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    Player owner;
    Location tractorBeamEnd;
    
    public Attachable attachment;
    
    public int lastX;
    public int lastY;
    public int diffX;
    public int diffY;
    
    private int countX = 0;
    private int countY = 0;
    
    private static final int max = 3;
    
    public TractorBeam(Player p){
    super(2, new GreenfootImage("tractor.png"));
    tractorBeamEnd = new Location(250,10,0);
    tractorBeamEnd.setOff(0,0);
    addAttatchmentPoint(tractorBeamEnd);
    owner = p;
    lastX = 500;
    lastY = 300;
    }
    
    
    public void act() 
    {
        
        
        double distanceX = Play.pnt.getX()-500;
        double distanceY = Play.pnt.getY()-300;
        
        int pointY = Play.pnt.getY();
        int pointX = Play.pnt.getX();
        
        int lX = diffX;
        int lY = diffY;
        
        diffX = pointX-lastX;
        diffY = pointY-lastY;
        
        if(diffX == 0 && lX != 0 && countX < max){
        countX++;
        diffX = lX;
        }else{
        countX = 0;
        }
        
        if(diffY == 0 && lY !=0 && countY < max){
        countY++;
        diffY = lY;
        }else{
        countY = 0;
        }
        
        //System.out.println(diffX + " , "+diffY);
        
        lastX = pointX;
        lastY = pointY;
        
        
        double distanceSquared = Math.pow(distanceX,2)+Math.pow(distanceY,2);
        
        int distance = (int)Math.sqrt(distanceSquared);
        if(distance > 150){
        distance = 150;
        }
        if(distance != 0){
        getImage().scale(16,distance*2);}
        
        int rotation =  (int)Math.toDegrees(Math.atan2(distanceX,distanceY));
        rotation = rotation * -1;
        rotation += 180;
        tractorBeamEnd.setRotation(rotation-90);
        
        double sin,cos,dstsqrt;
        double rads = Math.toRadians(rotation);
        /*sin = Math.toDegrees(Math.sin(rads));
        cos = Math.toDegrees(Math.cos(rads));
        int dx,dy;
        dstsqrt = Math.sqrt(distance);
        dx = (int)(cos*distance)+500;
        dy = (int)(sin*distance)+300;*/
         double xf = Math.cos((rotation-90) * 2 * Math.PI / 360) * distance;
         double yf = Math.sin((rotation-90) * 2 * Math.PI / 360) * distance;
        

         xf += 500;
         yf += 300;
        tractorBeamEnd.update((int)xf,(int) yf);
        
        //System.out.println(sin + " , "+cos);
        
        if(!(distance == 150) && Play.pnt.isTouching(Attachable.class)&& attachment == null){
         ArrayList<Attachable> hp16;
         hp16 = (ArrayList<Attachable>)Play.pnt.getIntersectingObjects(Attachable.class);
         for(int number = 0 ; number <hp16.size(); number++){
            if(hp16.get(number).type == Attachable.HULL_STRUCTURE || hp16.get(number).type == Attachable.BOMB){
            
                if(hp16.get(number).attached != null){
                    hp16.get(number).attached.isUsed = false;
                }
                hp16.get(number).attach(tractorBeamEnd);
             attachment = hp16.get(number);
             break;
            }
            
            }
         
           
             
        }
        if(attachment != null ){
        if(Attachable.getNearest(Play.pnt.getX(),Play.pnt.getY(), attachment) != null){
            Location YourMom = Attachable.getNearest(Play.pnt.getX(),Play.pnt.getY(), attachment);
            if(!YourMom.isUsed){
            int YourMomsEx  = YourMom.getRealX()-Play.pnt.getX();
            int YourSecondChromasome = YourMom.getRealY()-Play.pnt.getY();
            double YourWeight = Math.sqrt(Math.pow(YourMomsEx,2)+Math.pow(YourSecondChromasome,2));
            double aGoodWeight = 10.0;
            System.out.println(YourWeight);
            if(YourWeight > aGoodWeight){
            
            }else{
             attachment.dettach();
             attachment.attach(YourMom);
             attachment = null;
            }
            
            
        
        }}}
        
        
        setRotation(rotation);
        
    }
    
    public void dettach(){
    if(attachment != null){
        diffX = diffX/2;
        diffY = diffY/2;
        diffX -= Play.Mxr;
        diffY -= Play.Myr;
    
    
        if(diffX > 8){
            diffX = 7;
        }else if(diffX < -8){
           diffX = -7;
        }
        if(diffY > 8){
            diffY = 7 ;
        }else if(diffY < -8){
           diffY = -7;
        }
    
    
        attachment.setMomentum(diffX, diffY);
        //attachment.attached.isUsed = false;
        attached = null;
        isAttached = false;
        X = getX()-Play.getTrueX();
        Y = getY()-Play.getTrueY();
        if( attachment != null){
        attachment.dettach();}
        attachment = null;
    
    }
    
    }
    
    
}
